<template>
    <div class="app-container">
        <!-- 按钮区和搜索表单容器 -->
        <div class="tool-container">
            <!-- 按钮区 -->
            <div class="left-buttons">
                <el-button type="primary" @click="handleAdd">新增</el-button>
                <el-button type="danger">删除</el-button>
            </div>
            <!-- 搜索表单 -->
            <el-form :model="queryParams" ref="queryForm" :inline="true">
                <el-form-item>
                    <el-input v-model="queryParams.postName" placeholder="岗位名称" />
                </el-form-item>
                <el-form-item>
                    <el-input v-model="queryParams.postCode" placeholder="岗位编码" />
                </el-form-item>
                <el-form-item>
                    <el-select v-model="queryParams.status" style="width: 200px" placeholder="状态">
                        <el-option label="正常" value="0" />
                        <el-option label="停用" value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleQuery">搜索</el-button>
                    <el-button @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <!-- 数据表格 -->
        <el-table v-loading="loading" :data="postList" border>
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="岗位编号" prop="postId" />
            <el-table-column label="岗位名称" prop="postName" />
            <el-table-column label="岗位编码" prop="postCode" />
            <el-table-column label="排序" prop="postSort" />
            <el-table-column label="状态" prop="status">
                <template #default="scope">
                    <el-tag :type="scope.row.status === '0' ? 'success' : 'danger'">
                        {{ scope.row.status === '0' ? '正常' : '停用' }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" prop="createTime" />
            <el-table-column label="操作" align="center" width="200">
                <template #default="scope">
                    <el-button type="primary" size="small" @click="handleUpdate(scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 添加/修改岗位对话框 -->
        <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body destroy-on-close
            :close-on-click-modal="false" custom-class="post-dialog">
            <el-form ref="postForm" :model="postForm" :rules="rules" label-width="100px" class="post-form">
                <el-row :gutter="20">
                    <el-col :span="24">
                        <el-form-item label="岗位名称" prop="postName">
                            <el-input v-model="postForm.postName" placeholder="请输入岗位名称" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="岗位编码" prop="postCode">
                            <el-input v-model="postForm.postCode" placeholder="请输入岗位编码" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="岗位顺序" prop="postSort">
                            <el-input-number v-model="postForm.postSort" :min="0" controls-position="right"
                                style="width: 180px" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="岗位状态" prop="status">
                            <el-radio-group v-model="postForm.status">
                                <el-radio :value="'0'">正常</el-radio>
                                <el-radio :value="'1'">停用</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="postForm.remark" type="textarea" placeholder="请输入内容" :rows="4"
                                maxlength="200" show-word-limit />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="cancel" plain>取 消</el-button>
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import { getPostList, addPost, editPost, deletePost } from '@/api/system/post'

export default {
    name: 'SystemPost',
    data() {
        return {
            // 遮罩层
            loading: false,
            // 总条数
            total: 0,
            // 岗位表格数据
            postList: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                postName: undefined,
                status: undefined,
                postCode: undefined
            },
            // 弹出层标题
            dialog: {
                title: '',
                visible: false
            },
            // 表单参数
            postForm: {
                postId: undefined,
                postName: undefined,
                postCode: undefined,
                postSort: 0,
                status: '0',
                remark: undefined
            },
            // 表单校验
            rules: {
                postName: [
                    { required: true, message: '岗位名称不能为空', trigger: 'blur' }
                ],
                postCode: [
                    { required: true, message: '岗位编码不能为空', trigger: 'blur' }
                ],
                postSort: [
                    { required: true, message: '岗位顺序不能为空', trigger: 'blur' }
                ]
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {
        handleDelete(row) {
            this.$confirm('是否确认删除岗位"' + row.postName + '"', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deletePost(row.postId).then(res => {
                    if (res.data.code === 200) {
                        this.$message.success("删除成功")
                        this.getList()
                    } else {
                        this.$message.error(res.data.msg)
                    }
                })
            })
        },
        // 获取岗位列表
        async getList() {
            this.loading = true
            try {
                const response = await getPostList(this.queryParams)
                this.postList = response.data.data.records
                this.total = response.data.data.total
            } catch (error) {
                console.error(error)
            }
            this.loading = false
        },
        // 搜索按钮操作
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        // 重置按钮操作
        resetQuery() {
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                postName: undefined,
                status: undefined,
                postCode: undefined
            }
            this.handleQuery()
        },
        // 取消按钮
        cancel() {
            this.reset()
            this.dialog.visible = false
        },
        // 表单重置
        reset() {
            this.postForm = {
                postId: undefined,
                postName: undefined,
                postCode: undefined,
                postSort: 0,
                status: '0',
                remark: undefined
            }
            if (this.$refs.postForm) {
                this.$refs.postForm.resetFields()
            }
        },
        // 新增按钮操作
        handleAdd() {
            this.reset()
            this.dialog.visible = true
            this.dialog.title = "添加岗位"
        },
        // 修改按钮操作
        handleUpdate(row) {
            this.reset()
            this.postForm = { ...row }
            this.dialog.visible = true
            this.dialog.title = "修改岗位"
        },
        // 提交按钮
        submitForm() {
            this.$refs.postForm.validate(valid => {
                if (valid) {
                    if (this.postForm.postId) {
                        // 修改
                        editPost(this.postForm).then(res => {
                            if (res.data.code === 200) {
                                this.$message.success('修改成功')
                                this.dialog.visible = false
                                this.getList()
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                    } else {
                        // 新增
                        addPost(this.postForm).then(res => {
                            if (res.data.code === 200) {
                                this.$message.success('新增成功')
                                this.dialog.visible = false
                                this.getList()
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                    }
                }
            })
        }
    }
}
</script>
<style scoped>
.tool-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.left-buttons {
    display: flex;
    gap: 8px;
}

.el-form-item {
    margin-bottom: 16px;
}
</style>
